package com.fmsz.xmly.adapters.findrecom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.fmsz.xmly.R;
import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.findrecom.FindRecomBottomEntity;
import com.fmsz.xmly.entitys.findrecom.FindRecomGuessEntity;
import com.fmsz.xmly.entitys.findrecom.FindRecomSpecialEntity;
import com.fmsz.xmly.entitys.findrecom.GuessChildEntity;
import com.fmsz.xmly.entitys.findrecom.VP1DataEntity;
import com.fmsz.xmly.entitys.findrecom.VP2ChildEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.LinearLayoutManager.*;

/**
 * Created by Administrator on 2016/12/3.
 */

public class FindRecomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    InterfaceClass.FindRecomToClassifyCallback classifyCallback;

    private List<VP1DataEntity> vp1List;
    private List<FindRecomGuessEntity> guessList;
    private List<FindRecomGuessEntity> editorList;
    private List<FindRecomSpecialEntity> specialList;
    private List<FindRecomGuessEntity> hotList;
    private List<FindRecomBottomEntity> bottomList;
    private List<VP2ChildEntity> vp2ChildList;

    private List<ImageView> vp1ImageList = new ArrayList<>();
    private List<ImageView> bottomImage = new ArrayList<>();
    private RecyclerView recyclerView;
    private Context context;
    private LayoutInflater inflater;
    private static int vp1CurState;
    private static int bottomCurState;
    private static int vp1CurPosition;
    private static int bottomCurPosition;
    private int index;
    private RecyclerView.ViewHolder holder1 = null;
    private RecyclerView.ViewHolder holder2 = null;
    private static boolean vp1IsIdle = true;
    private static boolean bottomIsIdle = true;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1 && vp1ImageList.size() > 0) {
                int currentItem = vp1CurPosition;
                currentItem++;
                ((ViewHolderVP1) holder1).find_recommend_vp_1.setCurrentItem(currentItem);
            } else if (msg.what == 2 && vp1CurState == 0) {
                vp1IsIdle = true;
            } else if (bottomImage.size() > 0 && msg.what == 3) {
                int bottomItem = bottomCurPosition;
                bottomItem++;
                ((ViewHolderBottom) holder2).bottom_vp.setCurrentItem(bottomItem);
            } else if (msg.what == 4 && bottomCurState == 0) {
                bottomIsIdle = true;
            }
        }
    };

    public FindRecomAdapter(Context context, List<VP1DataEntity> vp1List, List<FindRecomGuessEntity> guessList, List<FindRecomGuessEntity> editorList, List<FindRecomSpecialEntity> specialList, List<FindRecomGuessEntity> hotList, List<FindRecomBottomEntity> bottomList, List<VP2ChildEntity> vp2ChildList,InterfaceClass.FindRecomToClassifyCallback callback) {
        this.vp1List = vp1List;
        this.guessList = guessList;
        this.editorList = editorList;
        this.specialList = specialList;
        this.hotList = hotList;
        this.bottomList = bottomList;
        this.vp2ChildList = vp2ChildList;
        this.classifyCallback=callback;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.find_recom_vp1, parent, false);
                holder = new ViewHolderVP1(view);
                holder1 = holder;
                startVP1Thread();
                break;
            case 1:
                view = inflater.inflate(R.layout.find_recom_guess, parent, false);
                holder = new ViewHolderGuess(view);
                break;
            case 3:
                view = inflater.inflate(R.layout.find_recom_special, parent, false);
                holder = new ViewHolderSpecial(view);
                break;
            case 24:
                view = inflater.inflate(R.layout.find_recom_checkmore, parent, false);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        classifyCallback.findRecomToClassifyCallback();
                    }
                });
                holder = new ViewHolderMore(view);
                break;
            case 25:
                view = inflater.inflate(R.layout.find_recom_bottom, parent, false);
                holder = new ViewHolderBottom(view);
                holder2 = holder;
                startBottomThread();
                break;
            case 2:
            default:
                view = inflater.inflate(R.layout.find_recom_editor, parent, false);
                holder = new ViewHolderEditor(view);
                break;
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                if (vp1List.size() > 0 && vp2ChildList.size()>0) {
                    getVP1ImageList();
                    ((ViewHolderVP1) holder).find_recommend_vp_1.setAdapter(new FindRecomVP1Adapter(context, vp1ImageList, vp1List));
                    ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(1);
                    ((ViewHolderVP1) holder).find_recommend_vp_1.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                        }

                        @Override
                        public void onPageSelected(int position) {
                            vp1CurPosition = position;
                            if (position == 0) {
                                ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(vp1ImageList.size() - 2, false);
                            } else if (position == vp1ImageList.size() - 1) {
                                ((ViewHolderVP1) holder).find_recommend_vp_1.setCurrentItem(1, false);
                            }
                            //TODO 根据vp1PointList,实现小点移动
                        /*for (int i = 0; i < vp1PointList.size(); i++) {
                            vp1PointList.get(i).setSelected(i == position);
                        }*/
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {
                            switch (state) {
                                case ViewPager.SCROLL_STATE_IDLE:
                                    vp1CurState = 0;
                                    handler.sendEmptyMessageDelayed(2, 0);
                                    break;
                                case ViewPager.SCROLL_STATE_DRAGGING:
                                    vp1IsIdle = false;
                                    vp1CurState = 1;
                                    break;
                            }
                        }
                    });
                    LinearLayoutManager manager=new LinearLayoutManager(context);
                    manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    ((ViewHolderVP1) holder).vp2_rv.setLayoutManager(manager);
                    ((ViewHolderVP1) holder).vp2_rv.setAdapter(new FindRecomVP2Adapter(context,vp2ChildList));
                }
                break;
            case 1:
                if (guessList.size() > 0) {
                    ((ViewHolderGuess) holder).guess_title.setText(guessList.get(0).getTitle());
                    ((ViewHolderGuess) holder).guess_more.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //TODO 给"更多"设置点击监听
                        }
                    });
                    ((ViewHolderGuess) holder).guess_gv.setAdapter(new FindRecomGuessAdapter(guessList, context));
                }
                break;
            case 2:
                if (editorList.size() > 0) {
                    ((ViewHolderEditor) holder).editor_title.setText(editorList.get(0).getTitle());
                    ((ViewHolderEditor) holder).editor_more.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //TODO 给"更多"设置点击监听
                        }
                    });
                    ((ViewHolderEditor) holder).editor_gv.setAdapter(new FindRecomEditorAdapter(editorList, context));
                }
                break;
            case 3:
                if (specialList.size() > 0) {
                    ((ViewHolderSpecial) holder).special_title.setText(specialList.get(0).getTitle());
                    ((ViewHolderSpecial) holder).special_more.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //TODO 给"更多"设置点击监听
                        }
                    });

                    ((ViewHolderSpecial) holder).special_rv.setAdapter(new FindRecomSpecialAdapter(specialList, context));
                }
                break;
            case 24:
                break;
            case 25:
                if (bottomList.size() > 0) {
                    getBottomImageList();
                    ((ViewHolderBottom) holder).bottom_vp.setAdapter(new FindRecomBottomAdapter(bottomImage));
                    ((ViewHolderBottom) holder).bottom_vp.setCurrentItem(1);
                    ((ViewHolderBottom) holder).bottom_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            bottomCurPosition = position;
                            if (position == 0) {
                                ((ViewHolderBottom) holder).bottom_vp.setCurrentItem(bottomImage.size() - 2, false);
                            } else if (position == bottomImage.size() - 1) {
                                ((ViewHolderBottom) holder).bottom_vp.setCurrentItem(1, false);
                            }
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {
                            switch (state) {
                                case ViewPager.SCROLL_STATE_IDLE:
                                    bottomCurState = 0;
                                    handler.sendEmptyMessageDelayed(4, 0);
                                    break;
                                case ViewPager.SCROLL_STATE_DRAGGING:
                                    bottomIsIdle = false;
                                    bottomCurState = 1;
                                    break;
                            }
                        }
                    });
                }
                break;
            default:
                if (hotList.size() > 0) {
                    ((ViewHolderEditor) holder).editor_title.setText(hotList.get(position - 4).getTitle());
                    ((ViewHolderEditor) holder).editor_more.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //TODO 给"更多"设置点击监听
                        }
                    });
                    ((ViewHolderEditor) holder).editor_gv.setAdapter(new FindRecomHotAdapter(hotList.get(position - 4).getList(), context));
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (hotList.size() > 0) {
            return 6 + hotList.size();
        }
        return 4;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
        insertLine();
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        recyclerView = null;
    }

    //发现-推荐-第一个子布局(ViewPager1)的ViewHolder
    public static class ViewHolderVP1 extends RecyclerView.ViewHolder {
        ViewPager find_recommend_vp_1;
        private RecyclerView vp2_rv;

        public ViewHolderVP1(View itemView) {
            super(itemView);
            find_recommend_vp_1 = (ViewPager) itemView.findViewById(R.id.find_recommend_vp_1);
            vp2_rv = (RecyclerView)itemView.findViewById(R.id.find_recom_vp2_rv);
            //TODO 添加小点给VP1
        }
    }

    //发现-推荐-第二个子布局(猜你喜欢)的ViewHolder
    public static class ViewHolderGuess extends RecyclerView.ViewHolder {
        GridView guess_gv;
        TextView guess_title, guess_more;

        public ViewHolderGuess(View itemView) {
            super(itemView);
            guess_gv = (GridView) itemView.findViewById(R.id.find_recom_guess_gv);
            guess_title = (TextView) itemView.findViewById(R.id.find_recom_guess_title);
            guess_more = (TextView) itemView.findViewById(R.id.find_recom_guess_more);
        }
    }

    //发现-推荐-第三个子布局(小编推荐)的ViewHolder
    public static class ViewHolderEditor extends RecyclerView.ViewHolder {
        GridView editor_gv;
        TextView editor_title, editor_more;

        public ViewHolderEditor(View itemView) {
            super(itemView);
            editor_gv = (GridView) itemView.findViewById(R.id.find_recom_editor_gv);
            editor_title = (TextView) itemView.findViewById(R.id.find_recom_editor_title);
            editor_more = (TextView) itemView.findViewById(R.id.find_recom_editor_more);
        }
    }

    //发现-推荐-第四个子布局(精品听单)的ViewHolder
    public static class ViewHolderSpecial extends RecyclerView.ViewHolder {
        RecyclerView special_rv;
        TextView special_title, special_more;

        public ViewHolderSpecial(View itemView) {
            super(itemView);
            special_rv = (RecyclerView) itemView.findViewById(R.id.find_recom_special_rv);
            special_title = (TextView) itemView.findViewById(R.id.find_recom_special_title);
            special_more = (TextView) itemView.findViewById(R.id.find_recom_special_more);
        }
    }

    //发现-推荐-第五个子布局(分类跳转)的ViewHolder
    public static class ViewHolderMore extends RecyclerView.ViewHolder {
        ImageView more_iv;

        public ViewHolderMore(View itemView) {
            super(itemView);
            more_iv = (ImageView) itemView.findViewById(R.id.find_recom_checkmore_iv);

        }
    }

    //发现-推荐-第六个子布局(底部ViewPager)的ViewHolder
    public static class ViewHolderBottom extends RecyclerView.ViewHolder {
        ViewPager bottom_vp;

        public ViewHolderBottom(View itemView) {
            super(itemView);
            bottom_vp = (ViewPager) itemView.findViewById(R.id.find_recom_bottom_vp);
        }
    }

    //开启VP1的子线程
    private void startVP1Thread() {
        //VP1图片轮播子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    while (vp1IsIdle) {
                        try {
                            Thread.sleep(3000);
                            if (vp1IsIdle) {
                                handler.sendEmptyMessageDelayed(1, 0);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    //开启底部ViewPager子线程
    private void startBottomThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    while (bottomIsIdle) {
                        try {
                            Thread.sleep(3000);
                            if (bottomIsIdle) {
                                handler.sendEmptyMessageDelayed(3, 0);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

    //实例化vp1ImageList
    private void getVP1ImageList() {
        if (vp1ImageList.size() > 0) {
            vp1ImageList.clear();
        }
        int[] imageArr = new int[vp1List.size() + 2];
        if (vp1List.size() > 0) {
            for (int i = 0; i < imageArr.length; i++) {
                ImageView image = new ImageView(context);
                image.setClickable(true);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                if (i == 0) {
                    Picasso.with(context).load(vp1List.get(vp1List.size() - 1).getPic()).into(image);
                } else if (i > 0 && i < imageArr.length - 1) {
                    Picasso.with(context).load(vp1List.get(i - 1).getPic()).into(image);
                } else if (i == imageArr.length - 1) {
                    Picasso.with(context).load(vp1List.get(0).getPic()).into(image);
                }
                vp1ImageList.add(image);
            }
        }
    }

    //获取底部ViewPager的ImageList
    private void getBottomImageList() {
        if (bottomImage.size() > 0) {
            bottomImage.clear();
        }
        int[] imageArr = new int[bottomList.size() + 2];
        if (bottomList.size() > 0) {
            for (int i = 0; i < imageArr.length; i++) {
                ImageView image = new ImageView(context);
                image.setClickable(true);
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                if (i == 0) {
                    Picasso.with(context).load(bottomList.get(bottomList.size() - 1).getCover()).into(image);
                } else if (i > 0 && i < imageArr.length - 1) {
                    Picasso.with(context).load(bottomList.get(i - 1).getCover()).into(image);
                } else if (i == imageArr.length - 1) {
                    Picasso.with(context).load(bottomList.get(0).getCover()).into(image);
                }
                bottomImage.add(image);
            }
        }
    }

    //设置分割线宽度
    private void insertLine() {
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0, 0, 0, 20);
            }
        };
        recyclerView.addItemDecoration(decoration);
    }

}
